﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharpWin;
using System.Windows.Forms;
using System.Drawing;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal class MeTabControlEx : TabControlEx
    {
        private int z;
        private Timer mTime;
        private int ac;
        TabControlEx ab;
        DateTime aa;
        public MeTabControlEx()
        {
            mTime = new Timer();
            mTime.Interval = 200;
            mTime.Tick += new EventHandler(mTime_Tick);
            this.MouseMove += new MouseEventHandler(MeTabControlEx_MouseMove); 
        }

      //  internal bool MonseMoveSelect { get; set; }

        void MeTabControlEx_MouseMove(object sender, MouseEventArgs e)
        {
            if (!DataFactory.SystemConfig.IsMoveAutoClickTab)
                return;
            if (Cal(this.aa, 50))
            {
                this.aa = DateTime.Now;
                TabControlEx ex = sender as TabControlEx;
                if (ex != null)
                {
                    Point pt = ex.PointToClient(Control.MousePosition);
                    bool flag = false;
                    for (int i = 0; i < ex.TabPages.Count; i++)
                    {
                        Rectangle tabRect = ex.GetTabRect(i);
                        if (tabRect.Contains(pt))
                        {
                            flag = true;
                            if (ex.SelectedIndex != i)
                            {
                                if ((this.z <= tabRect.Top) && (this.z >= tabRect.Bottom))
                                {
                                    ex.SelectedIndex = i;
                                }
                                else
                                {
                                    this.ab = ex;
                                    this.ac = i;
                                    this.mTime.Enabled = true;
                                    //Console.WriteLine("" + i);
                                    //   ex.SelectedIndex = i;
                                }
                            }
                        }
                    }
                    if (!flag)
                    {
                        this.z = pt.Y;
                    }
                }
            }
        }

        void mTime_Tick(object sender, EventArgs e)
        {
            try
            {
                TabControlEx ab = this.ab;
                if (ab != null)
                {
                    Point pt = ab.PointToClient(Control.MousePosition);
                    Rectangle tabRect = ab.GetTabRect(this.ac);
                    if (tabRect.Contains(pt) && (ab.SelectedIndex != this.ac))
                    {
                        ab.SelectedIndex = this.ac;
                        ab.Focus();
                        this.z = tabRect.Top;
                    }
                }
            }
            catch { }
            this.mTime.Enabled = false;
        }

        bool Cal(DateTime time, int a)
        {
            TimeSpan span = (TimeSpan)(DateTime.Now - time);
            long totalMilliseconds = (long)span.TotalMilliseconds;
            return (totalMilliseconds > a);
        }

        //void b(object sender, MouseEventArgs e)
        //{
        //    if (Cal(this.aa, 50))
        //    {
        //        this.aa = DateTime.Now;
        //        TabControlEx ex = sender as TabControlEx;
        //        if (ex != null)
        //        {
        //            Point pt = ex.PointToClient(Control.MousePosition);
        //            bool flag = false;
        //            for (int i = 0; i < ex.TabPages.Count; i++)
        //            {
        //                Rectangle tabRect = ex.GetTabRect(i);
        //                if (tabRect.Contains(pt))
        //                {
        //                    flag = true;
        //                    if (ex.SelectedIndex != i)
        //                    {
        //                        if ((this.z <= tabRect.Top) && (this.z >= tabRect.Bottom))
        //                        {
        //                            ex.SelectedIndex = i;
        //                        }
        //                        else
        //                        {
        //                            this.ab = ex;
        //                            this.ac = i;
        //                            mTime.Enabled = true;
        //                         //   ex.SelectedIndex = i;
        //                        }
        //                    }
        //                }
        //            }
        //            if (!flag)
        //            {
        //                this.z = pt.Y;
        //            }
        //        }
        //    }
        //}
    }
}